<?php
/**
 *author:韩帅
 *createtime:2017/6/19 14:10
 *description:
 */
namespace App\Http\Controllers\App;

use App\Models\Banner;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class BannerController extends BaseController
{
    private $position = [1=>'首页'];

    /**
     * banner图的展示
     */
    public function bannerList(Request $request)
    {

        $return = []; //返回的数据

        $title    = trim($request->input('title',''));
        $status   = $request->input('status','');
        $position = $request->input('position','1');

        $obj  = Banner::where('isdelete',Banner::ISDELETE_NO);

        if (!empty($title)) {
            $obj->where('title','like','%'.$title.'%');
        }
        if (!empty($status)) {
            $obj->where('status',$status);
        }
        if (empty($position)) {
            $obj->where('position',$position);
        }

        $data   = $obj->orderBy('display','asc')->paginate($this->pageSize)->toArray();
        $return = $data;

        if (empty($return)) {
            return self::json($return,'获取数据为空',200);
        }

        return self::json($return,'成功');
    }

    /**
     * banner图添加
     */
    public function bannerAdd(Request $request)
    {

        $title    = trim($request->input('title'),'');   //图片标题
        $img_url  = trim($request->input('img_url'),''); //图片路径
        $url      = trim($request->input('url'),'');     //跳转路径
        $position = $request->input('position',1);       // banner位置

        if(empty($title)) {
            return self::error('必填字段不能为空');
        }

        //判断是否重复添加
        $res = $this->bannerCheck($title);

        if ($res) {
            return self::error('不能重复添加');
        }

        $obj  =  new Banner();
        $obj->title    = $title;
        $obj->position = $position;
        $obj->img_url  = $img_url;
        $obj->url      = $url;

        $res = $obj->save();

        //排序的ID默认为主键ID
        $id = $obj->id;
        $obj->display = $id;

        $res = $obj->save();

        if ($res == true) {
            return self::success('成功');
        }
        return self::error('添加失败');
    }

    /**
     * banner图详情
     */
    public function bannerInfo(Request $request)
    {

        $id = $request->input('id','');

        if (empty($id)) {

            return self::error('ID不能为空');
        }

        $data = Banner::where('isdelete',Banner::ISDELETE_NO)->find($id);

        if (empty($data)) {
            return self::success('数据为空');
        }
        return self::json($data,'成功');
    }

    /**
     * banner图信息修改
     */
    public function bannerUpdate(Request $request)
    {
        $id       = $request->input('id','');
        $title    = trim($request->input('title'));
        $position = $request->input('position',1);
        $url      = trim($request->input('url'));
        $img_url  = trim($request->input('$img_url'));

        if (empty($id) || empty($title)) {

            return self::error('必填字段不能为空');
        }

        $obj =  Banner::where('isdelete',Banner::ISDELETE_NO)->find($id);

        if (empty($obj)) {

            return self::error('本条记录不存在');
        }

        $obj->title    = $title;
        $obj->url      = $url;
        $obj->img_url  = $img_url;
        $obj->position = $position;

        $res = $obj->save();

        if ($res == true) {

            return self::success('成功');
        }
        return self::error('失败');
     }

     /**
      * banner图是否启用
      */
    public function bannerStatus(Request $request)
    {

        $id          = $request->input('id','');
        $status      = $request->input('status',0);

        if (empty($id)) {
            return self::error('必填字段不能为空');
        }

        $obj =  Banner::where('isdelete',Banner::ISDELETE_NO)->find($id);

        if (empty($obj)) {
            return self::error('本条记录不存在');
        }

        $obj->status = $status;

        $res = $obj->save();

        if ($res == true) {
            return self::success('成功');
        }
        return self::error('状态更新失败');
    }

    /**
     * banner图删除
     */
    public function bannerDelete(Request $request)
    {

        $id = $request->input('id','');

        if (empty($id)) {
            return self::error('ID不能为空');
        }

        $obj =  Banner::where('isdelete',Banner::ISDELETE_NO)->find($id);

        if (empty($obj)) {
            return self::error('本条记录不存在');
        }

        $obj->status   = Banner::ISDELETE_NO;
        $obj->isdelete = Banner::ISDELETE_YES;

        $res = $obj->save();

        if ($res == true) {
            return self::success('成功');
        }
        return self::error('失败');
    }

    /**
     *验证是否重复
     */
    public function bannerCheck($title) {

        $obj = new Banner();
        $data = $obj->where('isdelete',Banner::ISDELETE_NO)->where('title',$title)->first();

        if (!empty($data)) {
            return true;
        }
        return false;
    }

    /**
     * banner排序
     */
    public function bannerOrder(Request $request) {

        $idA      = $request->input('idA','');
        $idB      = $request->input('idB','');
        $displayA = $request->input('displayA','');
        $displayB = $request->input('displayB','');

        if (empty($idA) || empty($idB) || empty($displayA) || empty($displayB)) {
            return self::error('必填字段不能为空');
        }

        //先判断该ID是否存在
        $objA = Banner::where('isdelete',Banner::ISDELETE_NO)->find($idA);
        $objB = Banner::where('isdelete',Banner::ISDELETE_NO)->find($idB);

        if (empty($objA) || empty($objB)) {
           return self::error('本记录不存在');
        }

        //开事务
        $res = false;
        DB::beginTransaction();
        try{
            $objA->display = $displayB;
            $objB->display = $displayA;
            $objA->save();
            $objB->save();
            // 提交事务
            DB::commit();
            $res = true;
        } catch (\Exception $e) {
            // 回滚事务
            DB::rollback();
        }

        if ($res == true) {
            return self::success('成功');
        }
        return self::json($e,400);
    }
}